package cn.mumzone.basic.net.http;

import android.text.TextUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import okhttp3.Call;
import okhttp3.OkHttpClient;

/**
 * Author Chaney
 * Date   2017/6/7 上午11:31.
 * EMail  qiuzhenhuan.hi@gmail.com
 */

public class RequestHelper {


    public static InputStream openStream(String url, Map<String, String> params) {
        try {
            OkHttpClient okHttpClient = ApiRequestHelper.getInstance().getOkHttpClient();
            StringBuffer buffer = new StringBuffer(url);
            if(params != null && !params.isEmpty()){
                for(String key : params.keySet()){
                    if(!buffer.toString().contains("?")){
                        buffer.append("?");
                    }else {
                        buffer.append("&");
                    }
                    String value = params.get(key);
                    if(!TextUtils.isEmpty(value)){
                        buffer.append(String.format("%s=%s",key,value));
                    }
                }
            }
            okhttp3.Request request = new okhttp3.Request.Builder()
                    .url(buffer.toString())
                    .get()
                    .build();
            Call call = okHttpClient.newCall(request);
            return call.execute().body().byteStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
